<?php //pack11class

class History extends Module {

    private $homePlaced=false;
    private $children=array();
    private $result=array();
    private $hiddenChildren=array();
    private $hiddenString="";
    private $currentFound=false;
    private $lastAvailable=false;

    public static $sqlQuery=array();
    public static $queryObject;
    public static $current;

	public function setup() {
        $this->traceEnabled=true;
        //GetQuery::translate($_GET["state"]);
        $level=PropertiesManager::getLevel(0);
        $group=PropertiesManager::getGroup(2);

        $home=new HistoryHomeItem;
        $home->setBody(LabelManager::getLabel("welcome"));

        self::$queryObject=new GetQueryObject;
        
        $availableChildren=array_merge($level, $group);
        $availableHidden=array();

         foreach($availableChildren as $property) {
             $value=($property->value)? $this->getValuesCount($property):$this->propertyUsable($property);
            if($value) $property->value=$value[1];
            if($property->value && !$property->isValue) {
                $this->lastAvailable=$property;
                array_push(self::$sqlQuery,"`{$property->field}`='{$property->value}'");
                $item=new HistoryItem;
                $item->count=$value[0];
                $item->value=ValuesManager::getValueByID($property->value);
                $item->property=$property;
                array_push($this->children,$item);
            }
        }

        $this->lastAvailable=false;
        $prw=(array_key_exists("preview",$_GET))? "&preview":"";
        if(count($this->children)>0 || array_key_exists("search", $_GET)) $home->setBody('<a href="?state='.MD5("start").$prw.'">'.LabelManager::getLabel("start").'</a>&nbsp;»&nbsp;');
              $this->addChild($home);

              if(array_key_exists("search", $_GET)){
                  $child=new HistoryCurrentItem;
                  $child->setBody(LabelManager::getLabel("search_history_label"));
                  $child->count=count(ObjectsManager::getObjects());
                  $this->addChild($child);
                  return;
              }

        for($i=0; $i<count($this->children); $i++) {
               $child = ($i<count($this->children)-1)? new HistoryLinkItem:new HistoryCurrentItem;
               $child->setBody($this->children[$i]->value);
               $child->count=$this->children[$i]->count;
               self::$queryObject->setValue($this->children[$i]->property);
               $link=GetQuery::generate(self::$queryObject);
               $child->link="?state=".$link;
               $this->addChild($child);
               if($i==count($this->children)-1) self::$current=$this->children[$i]->property;
        }

        //print_r($this->children);
    }

    private function propertyUsable(Property $property) {
        $parent=($this->lastAvailable)? $this->lastAvailable:PropertiesManager::getParent($property);
        if(!$parent) return false;
        if(!$parent->value) {
            //trace($parent." has no value");
            return false;
        }
        $whereString=(count(self::$sqlQuery)>0) ? "where ".implode(" AND ",self::$sqlQuery)." AND `active`='1'":" where `active`='1'";
        $parentValues=MySQLGet::process("Select count(`{$parent->field}` is not null) as `count`, `{$parent->field}` as `value` from ".MySQL::TABLE_OBJECTS." {$whereString} group by `{$parent->field}`;");
        $propertyValues=MySQLGet::process("Select count(`{$property->field}` is not null) as `count`, `{$property->field}` as `value` from ".MySQL::TABLE_OBJECTS." {$whereString} group by `{$property->field}`;");

        //trace(MySQLGet::getQuery());

        if($parentValues[0]["count"]==$propertyValues[0]["count"] && !is_null($propertyValues[0]["value"])) {
            return array($propertyValues[0]["count"],$propertyValues[0]["value"]);
        }

        return false;
    }

    private function getValuesCount(Property $property) {
        $whereString=(count(self::$sqlQuery)>0) ? "where ".implode(" AND ",self::$sqlQuery)." AND `{$property->field}`='{$property->value}'":" where `{$property->field}`='{$property->value}'";
        $values=MySQLGet::process("Select `id` from ".MySQL::TABLE_OBJECTS." {$whereString} AND `active`='1';");
        return array(MySQLGet::$cc,$property->value);
    }

    private function processChild(Property $property) {
        if($property->value) array_push(self::$sqlQuery,"`{$property->field}`='{$property->value}'");
        //trace($property."    ".implode(" AND ",self::$sqlQuery));

        $whereString=(count(self::$sqlQuery)>0) ? "where ".implode(" AND ",self::$sqlQuery):"";
        $currentChildren=$this->getValues($property, $whereString);

        $child=PropertiesManager::getChild($property);
        if(!$child) return false;

        $childWhere=($child->value)? " AND `{$child->field}`='{$child->value}'":"";
        $whereString=(count(self::$sqlQuery)>0) ? "where ".implode(" AND ",self::$sqlQuery).$childWhere:" where `{$child->field}`='{$child->value}'";
        $nextChildren=$this->getValues($child, $whereString);

       //trace($currentChildren." <=> ".$nextChildren);
        return ($currentChildren==$nextChildren || $property->value)? $currentChildren:false;
    }



}
?>
